package com.jchao;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

/**
 * create 2023/11/16 17:20
 */


@SpringBootTest
public class ProcessTest01 {

    @Autowired
    private RepositoryService repositoryService;


    @Test
    public void deployProcess(){
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/qingjia.bpmn20.xml")
                .addClasspathResource("process/qingjia.png")
                .name("请假申请流程")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());

    }

    @Test
    public void deployProcessByZip(){
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("process/qingjia.zip");
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        Deployment deploy = repositoryService.createDeployment()
                .addZipInputStream(zipInputStream)
                .name("请假申请流程02")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    @Autowired
    private RuntimeService runtimeService;

    @Test
    public void startUpProcess(){
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("qingjia");
        System.out.println(processInstance.getProcessDefinitionId());
        System.out.println(processInstance.getId());
        System.out.println(processInstance.getActivityId());
    }

    @Autowired
    private TaskService taskService;

    @Test
    public void findPendingTaskList(){
        String assign = "jack";
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee(assign)
                .list();
        for (Task task : list) {
            System.out.println("流程定义ID："+task.getProcessDefinitionId());
            System.out.println("任务D："+task.getId());
            System.out.println("流程实例ID："+task.getProcessInstanceId());
            System.out.println("任务名称："+task.getName());
        }
    }

    @Test
    public void completeTask(){
        String assign = "wangwu";
        Task task = taskService.createTaskQuery()
                .taskAssignee(assign)
                .singleResult();
        taskService.complete(task.getId());
    }

    @Autowired
    private HistoryService historyService;

    @Test
    public void findHistoryTask(){
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
                .taskAssignee("zhangsan")
                .finished()
                .list();
        for (HistoricTaskInstance historicTaskInstance : list) {
            System.out.println(historicTaskInstance.getId());
            System.out.println(historicTaskInstance.getProcessInstanceId());
            System.out.println(historicTaskInstance.getProcessDefinitionId());
            System.out.println(historicTaskInstance.getName());
            System.out.println(historicTaskInstance.getAssignee());
        }
    }

    @Test
    public void suspendProcessDefinition(){
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId("qingjia:3:f27fd78f-8462-11ee-91b5-bce92ff92557")
                .list();
        for (ProcessDefinition processDefinition : list) {
            boolean suspended = processDefinition.isSuspended();
            if (suspended) {
                System.out.println("执行激活");
                repositoryService.activateProcessDefinitionById(processDefinition.getId(), true, null);
            }else {
                System.out.println("执行挂起");
                repositoryService.suspendProcessDefinitionById(processDefinition.getId(), true, null);
            }
        }
    }

    @Test
    public void suspendProcessInstance(){
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId("6d02f374-8464-11ee-92c0-bce92ff92557")
                .singleResult();
        boolean suspended = processInstance.isSuspended();
        if (suspended) {
            System.out.println("执行激活");
            runtimeService.activateProcessInstanceById(processInstance.getId());
        }else {
            System.out.println("执行挂起");
            runtimeService.suspendProcessInstanceById(processInstance.getId());
        }
    }



    /**
     * 删除流程定义
     */

    @Test
    public void deleteDeployment() {
        //部署id
        String deploymentId = "ff8033b6-8461-11ee-ab8d-bce92ff92557";
        //删除流程定义，如果该流程定义已有流程实例启动则删除时出错
        repositoryService.deleteDeployment(deploymentId);
        //设置true 级联删除流程定义，即使该流程有流程实例启动也可以删除，设置为false非级别删除方式
        //repositoryService.deleteDeployment(deploymentId, true);
    }
}


